package com.gx.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gx.common.result.AppResult;
import com.gx.pojo.PageSysInfoDto;
import com.gx.pojo.SysLogInfo;
import com.gx.service.SysLogInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName OperationController
 * @Author lpj
 * @Date 2022/5/6 19:24
 **/
@RestController
@Api(tags = "日志")
@RequestMapping(value = "/sysInfo")
@Slf4j
@AllArgsConstructor
public class SysLogInfoController {

    private final SysLogInfoService sysLogInfoService;

    /**
     * 分页显示事件列表
     */
    @ApiOperation("查询日志列表")
    @PostMapping("/findAll")
    public AppResult<?> findAllOperationAdmin(
            @ApiParam(value = "分页信息") @RequestBody(required = false) PageSysInfoDto dto
    ) {
        dto.checkPageParam();
        Page<SysLogInfo> param = new Page<>(dto.getCurrPage(), dto.getPageSize());
        LambdaQueryWrapper<SysLogInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysLogInfo::getCreatedTime);
        if (StrUtil.isNotEmpty(dto.getTypeName())) {
            queryWrapper.like(SysLogInfo::getType, dto.getTypeName());
        }
        if (StrUtil.isNotEmpty(dto.getCode())) {
            queryWrapper.like(SysLogInfo::getCode, dto.getCode());
        }
        if (dto.getAll()) {
            Page<SysLogInfo> page = new Page<>();
            List<SysLogInfo> list = sysLogInfoService.list(queryWrapper);
            page.setTotal(list != null ? list.size() : 0);
            page.setRecords(list);
            return AppResult.success(page);
        }
        Page<SysLogInfo> page = sysLogInfoService.page(param, queryWrapper);
        return AppResult.success(page);
    }

    /**
     * 分页显示事件列表
     */
    @ApiOperation("清空日志")
    @PostMapping("/delAll")
    public AppResult<?> delAll() {
        sysLogInfoService.remove(new LambdaQueryWrapper<>());
        return AppResult.success();
    }


}
